﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bordspil.Models;
using System.Web.Security;

namespace Bordspil.Controllers
{
    public class TicTacToeController : Controller
    {
        private IAppRepository m_repository = null;
        public TicTacToeController()
        {
            m_repository = new AppRepository();
        }

        public TicTacToeController(IAppRepository rep)
        {
          m_repository = rep;
        }
        //
        // GET: /Game/

        public ActionResult Index(int? id)
        {
            //naekmavelega sama virkni i tessu falli og i Index fallinu i KindoController
            if (id == null)
            {
                GameInstance newGame = new GameInstance();
                newGame.GameId = 2;
                newGame.PlayerName = User.Identity.Name;
                newGame.IsActive = true;
                newGame.NumberOfPlayers = 1;
                m_repository.AddGame(newGame);
                m_repository.Save();

                return View(newGame);
            }
            var result = (from game in m_repository.GetGames()
                          where game.InstanceId == id
                          select game).SingleOrDefault();

            if (result.NumberOfPlayers >= result.Games.MaxPlayers)
            {
                return View("FullGame");
            }
            result.NumberOfPlayers += 1;
            m_repository.Save();
            if (result != null)
            {
                return View(result);
            }

            return View("NotFound");
        }

        public JsonResult WhosTurnIsIt(int? groupId, string userName)
        {
            var result = (from u in m_repository.GetPlayerList()
                          where u.id == groupId
                          select u);

            result.FirstOrDefault().IsItMyTurn = true;
            m_repository.Save();

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /*
        public JsonResult RateTicTacToe(string UserName, string Value)
        {
            if (Value == null)
            {
                return Json("tomt", JsonRequestBehavior.AllowGet);
            }
            //Vel notandan sem mogulega hefur gefid leik einkun adur
            var hasRated = (from r in m_repository.GetAllGameRankings()
                            where r.UserName == UserName
                            select r).SingleOrDefault();

            //ef hann hefur gefid einkun aetti ad koma null og ta hefur notandi gefid einkun adur
            //og getur tad ekki aftur
            if (hasRated != null)
            {
                return Json("Buin", JsonRequestBehavior.AllowGet);
            }
            else
            {
                //annars verdur til nytt tilvik af GameRankin og tad sett i database
                GameRanking newGameRank = new GameRanking();
                newGameRank.GameId = 2;
                newGameRank.UserName = UserName;

                if (Value == "+")
                {
                    newGameRank.Rating += 1;
                }
                else
                {
                    newGameRank.Rating -= 1;
                }

                m_repository.AddGameRating(newGameRank);
                m_repository.Save();
            }

            //vel leikinn sem notandi vill gefa einkun
            var result = (from games in m_repository.GetAllGames()
                          where games.Id == 2
                          select games).SingleOrDefault();

            if (Value == "+")
            {
                result.Rating += 1;
            }
            else if (Value == "-")
            {
                result.Rating -= 1;
            }

            //vista database breytinganar
            m_repository.Save();

            return Json(Value, JsonRequestBehavior.AllowGet);
        }
         */

    }
}
